package hos.java.util;


import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * <p>Title: IDUtils </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2021/4/1 23:26
 */
public class IDUtils {

    @SuppressWarnings("NewApi")
    public static final String getUniqueID() {
        int t1 = (int)(System.currentTimeMillis() / 1000L);
        int t2 = (int)System.nanoTime();
        int t3 = (new Random()).nextInt();
        int t4 = (new Random()).nextInt();
        byte[] b1 = NumberUtils.getBytes(t1);
        byte[] b2 = NumberUtils.getBytes(t2);
        byte[] b3 = NumberUtils.getBytes(t3);
        byte[] b4 = NumberUtils.getBytes(t4);
        byte[] bUniqueID = new byte[16];
        System.arraycopy(b1, 0, bUniqueID, 0, 4);
        System.arraycopy(b2, 0, bUniqueID, 4, 4);
        System.arraycopy(b3, 0, bUniqueID, 8, 4);
        System.arraycopy(b4, 0, bUniqueID, 12, 4);
        return Base64.getEncoder().encodeToString(bUniqueID);
    }

    public static String getUuid16() {
        String uuid = UUID.randomUUID().toString();
        char[] cs = new char[32];
        char c = 0;
        for (int i = uuid.length() / 2, j = 1; i-- > 0; ) {
            if ((c = uuid.charAt(i)) != '-') {
                cs[j++] = c;
            }
        }
        return String.valueOf(cs);
    }

    /**
     * 返回一个UUID
     *
     * @return 小写的UUID
     */
    public static String genUuid() {
        return UUID.randomUUID().toString();

    }

    /**
     * 返回一个UUID
     *
     * @return 大写的UUID
     */
    public static String genUUID() {
        return UUID.randomUUID().toString().toUpperCase();
    }

    /**
     * 返回一个有序列的uuid编码
     * 前11位为时间(毫秒)
     * 中间4位为主机特征码
     * 剩下的保证其唯一性
     *
     * @param hostFeature 主机特征码建议设置4位
     * @return
     */
    public static String getQuid(String hostFeature) {
        long date = System.currentTimeMillis();
        String s = UUID.randomUUID().toString();
        String str = Long.toHexString(date);
        String result = str + hostFeature
                + s.substring(17, 18) + s.substring(19, 23) + s.substring(24);
        return result.toUpperCase();
    }

    public static String genDateTime() {
        @SuppressWarnings("SimpleDateFormat")
        String time = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        if (time.contains("-")) {
            time = time.replaceAll("-", "");
        }
        if (time.contains(" ")) {
            time = time.replaceAll(" ", "");
        }
        if (time.contains(".")) {
            time = time.replaceAll("\\.", "");
        }
        return time;
    }

    public static String genDateUUID() {
        return genDateTime() + genUuid();
    }

    public static String genDateNanoId() {
        return genDateTime() + genNanoId();
    }

    public static String genNanoId() {
        return NanoIdUtils.randomNanoId();
    }

}
